package com.sls.web.manager.game.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.game.constants.ResultCode;
import com.sls.web.manager.game.pojo.ekhui.Deposit;
import com.sls.web.manager.pojo.UserInfoVO;

public class DepositClient {
	private static Logger log = LoggerFactory.getLogger(DepositClient.class) ;
	private final static Lock lock = new ReentrantLock() ;
	private volatile static DepositClient instance = null ;
	private DepositClient(){
		super() ;
	}
	public static DepositClient getInstance(){
		if(instance == null){
			try {
				lock.lock();
				if (instance == null) {
					instance = new DepositClient();
				}
			} finally{
				lock.unlock() ;
			}
		}
		return  instance ;
	}
	public Deposit getDeposit(Deposit deposit , UserInfoVO userInfoVO){
		Deposit resDeposit = null ;
		Head head = new Head();
		head.setServiceCode("300005");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setRequestId(UUID.randomUUID().toString());
		
		SocketDTO dto = SocketUtil.send(head, deposit) ;
		if(ResultCode.success.getCode().equals(dto.getHead().getResponseCode())){
			String body = dto.getBody() ;
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				resDeposit = gson.fromJson(body, new TypeToken<Deposit>(){}.getType()) ;
			}
		}
		return resDeposit ;
	}
	public List<Deposit> getDepositList(Deposit deposit , String startTime , 
			String endTime, int pageNo , int pageSize , UserInfoVO userInfoVO){
		List<Deposit> paymentList = null ;
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setServiceCode("300014");
		head.setUserId(userInfoVO.getUserId());
		head.setRequestId(UUID.randomUUID().toString());
		
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("deposit", deposit) ;
		paramMap.put("pageNo", pageNo) ;
		paramMap.put("pageSize", pageSize) ;
		paramMap.put("startTime", startTime) ;
		paramMap.put("endTime", endTime) ;
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
	    	String body = socketDTO.getBody();
	    	if(StringUtils.isNotBlank(body)){
	    		Gson gson = new Gson();
	    		paymentList = gson.fromJson(body , new TypeToken<List<Deposit>>(){}.getType()); 
	    	}
	    }
		return paymentList ;
	}
	public int getDepositCount( Deposit deposit, String startTime , 
			String endTime, UserInfoVO userInfoVO){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setServiceCode("300015");
		head.setUserId(userInfoVO.getUserId());
		head.setRequestId(UUID.randomUUID().toString());
		
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("deposit", deposit) ;
		paramMap.put("startTime", startTime) ;
		paramMap.put("endTime", endTime) ;
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				return gson.fromJson(body, new TypeToken<Integer>(){}.getType()) ;
			}
		}
		return 0 ;
	}
	public boolean checkDeposit( Deposit deposit ,UserInfoVO userInfoVO){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setServiceCode("300022");
		head.setUserId(userInfoVO.getUserId());
		head.setRequestId(UUID.randomUUID().toString());
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("deposit", deposit) ;
		paramMap.put("userInfoVO", userInfoVO) ;
		deposit.setOperatorId(userInfoVO.getUserId()) ;
		deposit.setOperatorId(userInfoVO.getUserName()) ;
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				return gson.fromJson(body, new TypeToken<Boolean>(){}.getType()) ;
			}
		}
		return false ;
	}
}
